package com.atbeijing.dwpublisher.controller;

import com.alibaba.fastjson.JSONObject;
import com.atbeijing.dwpublisher.service.PublisherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/")
public class PublisherController {

    @Autowired
    private PublisherService publisherService;

    /**
     * 封装总数数据
     * @param date
     * @return
     */
    @RequestMapping("/realtime-total")
    @ResponseBody
    public String realtimeTotal(@RequestParam("date") String date) {
        //创建list集合存放最终数据
        ArrayList<Map> result = new ArrayList<>();

        //获取日活总数
        Integer dauTotal = publisherService.getDauTotal(date);
        //获取新增交易额总数
        Double amountTotal = publisherService.getOrderAmountTotal(date);

        //创建存放新增日活的map集合
        HashMap<String, Object> dauMap = new HashMap<>();
        dauMap.put("id", "dau");
        dauMap.put("name", "新增日活");
        dauMap.put("value", dauTotal);

        //创建存放新增设备的map集合
        HashMap<String, Object> devMap = new HashMap<>();
        devMap.put("id", "new_mid");
        devMap.put("name", "新增设备");
        devMap.put("value", 233);

        //创建存放新增交易额的map集合
        HashMap<String, Object> gmvMap = new HashMap<>();
        gmvMap.put("id", "order_amount");
        gmvMap.put("name", "新增交易额");
        gmvMap.put("value", amountTotal);

        result.add(dauMap);
        result.add(devMap);
        result.add(gmvMap);

        return JSONObject.toJSONString(result);
    }

    /**
     * 封装分时数据
     * @param id
     * @param date
     * @return
     */
    @RequestMapping("realtime-hours")
    @ResponseBody
    public String realtimeHours(@RequestParam("id") String id, @RequestParam("date") String date) {
        //获取昨天的日期
        String yesterday = LocalDate.parse(date).plusDays(-1).toString();

        Map yesterdayHourMap = null;
        Map todayHourMap = null;
        if ("dau".equals(id)) {
            //获取昨天数据
            yesterdayHourMap = publisherService.getDauHours(yesterday);
            //获取今天日活数据
            todayHourMap = publisherService.getDauHours(date);
            //获取今天日活数据
        } else if ("order_amount".equals(id)) {
            //获取昨天交易额数据
            yesterdayHourMap = publisherService.getOrderAmountHourMap(yesterday);
            //获取今天交易额数据
            todayHourMap = publisherService.getOrderAmountHourMap(date);
        }


        //创建map集合用于存放结果数据
        HashMap<String, Object> result = new HashMap<>();
        result.put("yesterday", yesterdayHourMap);
        result.put("today", todayHourMap);

        return JSONObject.toJSONString(result);
    }


    /**
     * 灵活查询
     */
    @RequestMapping("sale_detail")
    @ResponseBody
    public String saleDetail(@RequestParam("date") String date,
                             @RequestParam("startpage") Integer startpage,
                             @RequestParam("size") Integer size,
                             @RequestParam("keyword") String keyword) throws IOException {

        Map<String, Object> result = publisherService.getSaleDetail(date, startpage, size, keyword);
        return JSONObject.toJSONString(result);
    }

}
